#include "lzkeyboard.h"
#include <QMessageBox>
#include <QDebug>

LzKeyBoard::LzKeyBoard(QWidget *parent) : QDialog(parent)
{
    m_strCorrectPasswd = "";
    m_strInput = "";

    m_bResult = false;

    this->setStyleSheet("QLineEdit{min-height: 30px} "
                        "QPushButton{border-image:none;background-color:rgb(220,220,220);color:rgb(0,0,0);"
                        "max-width:60px;min-width:60px;max-height:60px;min-height:60px;"
                        "padding:0px;}");

    m_layout = new QGridLayout();
    initKeyBoard();
    m_layout->setSpacing(10);
    m_layout->setMargin(10);
    this->setLayout(m_layout);

    m_InputBox->setText(m_strInput);

    connect(&buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(keyClicked(int)));
}

void LzKeyBoard::setCorrectPasswd(QString strPasswd)
{
    m_strCorrectPasswd = strPasswd;
}

bool LzKeyBoard::getResult()
{
    return m_bResult;
}

void LzKeyBoard::initKeyBoard()
{
    m_InputBox = new QLineEdit();
    m_InputBox->setReadOnly(true);
    m_InputBox->setEchoMode(QLineEdit::Password);
    m_layout->addWidget(m_InputBox, 0, 0, 1, 4);

    QPushButton* btnNumber_0 = new QPushButton("0");

    buttonGroup.addButton(btnNumber_0, Qt::Key_0);

    QPushButton* btnNumber_1 = new QPushButton("1");
    buttonGroup.addButton(btnNumber_1, Qt::Key_1);
    QPushButton* btnNumber_2 = new QPushButton("2");
    buttonGroup.addButton(btnNumber_2, Qt::Key_2);
    QPushButton* btnNumber_3 = new QPushButton("3");
    buttonGroup.addButton(btnNumber_3, Qt::Key_3);
    QPushButton* btnNumber_4 = new QPushButton("4");
    buttonGroup.addButton(btnNumber_4, Qt::Key_4);
    QPushButton* btnNumber_5 = new QPushButton("5");
    buttonGroup.addButton(btnNumber_5, Qt::Key_5);
    QPushButton* btnNumber_6 = new QPushButton("6");
    buttonGroup.addButton(btnNumber_6, Qt::Key_6);
    QPushButton* btnNumber_7 = new QPushButton("7");
    buttonGroup.addButton(btnNumber_7, Qt::Key_7);
    QPushButton* btnNumber_8 = new QPushButton("8");
    buttonGroup.addButton(btnNumber_8, Qt::Key_8);
    QPushButton* btnNumber_9 = new QPushButton("9");
    buttonGroup.addButton(btnNumber_9, Qt::Key_9);
    QPushButton* btnColon = new QPushButton(tr("."));
    buttonGroup.addButton(btnColon, Qt::Key_Colon);
    btnColon->setEnabled(false);
    QPushButton* btnBackSpace = new QPushButton(tr("Delete"));
    buttonGroup.addButton(btnBackSpace, Qt::Key_Backspace);
    QPushButton* btnCancel = new QPushButton(tr("Cancel"));
    buttonGroup.addButton(btnCancel, Qt::Key_Cancel);
    QPushButton* btnEnter = new QPushButton("Enter");
    buttonGroup.addButton(btnEnter, Qt::Key_Enter);

    m_layout->addWidget(btnNumber_7,    1,0,1,1);
    m_layout->addWidget(btnNumber_8,    1,1,1,1);
    m_layout->addWidget(btnNumber_9,    1,2,1,1);
    m_layout->addWidget(btnBackSpace,   1,3,1,1);

    m_layout->addWidget(btnNumber_4,    2,0,1,1);
    m_layout->addWidget(btnNumber_5,    2,1,1,1);
    m_layout->addWidget(btnNumber_6,    2,2,1,1);
    m_layout->addWidget(btnCancel,      2,3,1,1);

    m_layout->addWidget(btnNumber_1,    3,0,1,1);
    m_layout->addWidget(btnNumber_2,    3,1,1,1);
    m_layout->addWidget(btnNumber_3,    3,2,1,1);
    m_layout->addWidget(btnEnter,       3,3,2,1);

    m_layout->addWidget(btnNumber_0,    4,0,1,2);
    m_layout->addWidget(btnColon,       4,2,1,1);

    btnNumber_0->setStyleSheet("QPushButton{max-width:130px;min-width:130px;max-height:60px;min-height:60px;}");
    btnEnter->setStyleSheet("QPushButton{max-width:60px;min-width:60px;max-height:130px;min-height:130px;}");
}

void LzKeyBoard::keyClicked(int id)
{
    switch (id)
    {
    case Qt::Key_Colon:
        m_strInput += '.';
        break;
    case Qt::Key_Backspace:
        m_strInput.chop(1);
        break;
    case Qt::Key_Cancel:
        clickCancel();
        break;
    case Qt::Key_Enter:
        clickEnter();
        break;
    default:
        m_strInput += QString::number(id-Qt::Key_0);
        break;
    }

    m_InputBox->setText(m_strInput);
}

void LzKeyBoard::clickCancel()
{
    m_bResult = false;
    this->close();
}

void LzKeyBoard::clickEnter()
{
    m_bResult = (m_strInput == m_strCorrectPasswd);
    if(!m_bResult)
    {
        QMessageBox::warning(this,
                             tr("错误提示"),
                             tr("密码错误,请重新输入"),
                             QMessageBox::Ok);
    }
    else
    {
        this->close();
    }

}
